package com.ls.lserp.modules.material.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ls.lserp.modules.sys.utils.SystemMessageUtils;
import org.apache.commons.collections.map.HashedMap;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ls.lserp.common.config.Global;
import com.ls.lserp.common.persistence.AjaxDone;
import com.ls.lserp.common.persistence.Page;
import com.ls.lserp.common.utils.ResourcesUtil;
import com.ls.lserp.common.utils.StringUtils;
import com.ls.lserp.common.web.BaseController;
import com.ls.lserp.modules.material.entity.SlopworkInbound;
import com.ls.lserp.modules.material.service.SlopworkInboundService;

import freemarker.template.Configuration;
import groovy.util.ResourceException;

@Controller
@RequestMapping(value = "${adminPath}/slopwork/inbound/")
public class SlopworkInboundController extends BaseController {

	// 保存 编辑 删除 方法
	@Autowired
	private SlopworkInboundService slopworkInboundService;
	@Autowired
	Configuration freemarkerConfiguration;

	@ModelAttribute
	public SlopworkInbound get(@RequestParam(required = false) String inventoryRequestDetailId) {

		// 判断id是否为空
		if (StringUtils.isNotBlank(inventoryRequestDetailId)) {
			// 执行查询语句 查询基本信息
			return slopworkInboundService.get(Long.parseLong(inventoryRequestDetailId));
		} else {
			// 若为空，实例化
			return new SlopworkInbound();
		}
	}

	/**
	 * @Title: 画面跳转用
	 * @return url
	 */
	@RequiresPermissions("slopwork:inbound:view")
	@RequestMapping(value = { "" })
	public String list(Model model) {
		// 画面跳转
		return "modules/material/slopworkInbound";
	}

	/**
	 * @Title: 画面检索
	 * @Description: 查询页面
	 * @param slopworkInbound
	 * @param request
	 * @param response
	 * @param model
	 * @return searchList
	 * @throws Exception
	 */
	@RequiresPermissions("slopwork:inbound:view")
	@RequestMapping(value = { "searchList" })
	@ResponseBody
	public String searchList(SlopworkInbound slopworkInbound, HttpServletRequest request, HttpServletResponse response,
			Model model) throws Exception {
		Map<Object, Object> listMap = new HashMap<Object, Object>();
		// ajax回调时使用的一览list名
		request.setAttribute(Global.LIST_NAME, "slopworkInbound");
		// 拼接字符串供分页使用
		StringBuilder searchParam = new StringBuilder();
		// 入库单号
		searchParam.append("&inventoryRequestNo=");
		searchParam.append(slopworkInbound.getInventoryRequestNo());
		// 客户款号
		searchParam.append("&customerStyleNo=");
		searchParam.append(slopworkInbound.getCustomerStyleNo());
		// 日期
		searchParam.append("&dateFrom=");
		searchParam.append(slopworkInbound.getDateFrom());
		searchParam.append("&dateTo=");
		searchParam.append(slopworkInbound.getDateTo());
		// 状态
		searchParam.append("&tabPageStatus=");
		searchParam.append(slopworkInbound.getTabPageStatus());
		// 放到Attribute里，使用Global类里面的SEARCH_PAEAM作为key
		request.setAttribute(Global.SEARCH_PAEAM, searchParam.toString());
		
		// 查询一览页面数据
		Page<SlopworkInbound> page = slopworkInboundService.findList(new Page<SlopworkInbound>(request, response),slopworkInbound);
		// 读取系统路径
		String strUri = request.getContextPath() + Global.getAdminPath();
		// 读取文件生成字符串
		String strHtml = "";
		Map<String, Object> map = new HashedMap();
		map.put("adminUri", strUri);
		map.put("slopworkInbounds", page.getList());
		map.put("pageNo", page.getPageNo());
		map.put("pageSize", page.getPageSize());
		strHtml = FreeMarkerTemplateUtils
				.processTemplateIntoString(freemarkerConfiguration.getTemplate("slopworkInbound.ftl"), map);
		listMap.put("slopworkInbound", strHtml);
		listMap.put("page", page.getHtml());

		// ajax返回结果
		return renderString(response, listMap);
	}
	
	/**
	    * 进入查看页面
	    * @param slopworkInbound
	    * @param model
	    * @return
	    */
	   @RequiresPermissions("slopwork:inbound:view")
	   @RequestMapping(value = "show")
	   public String show(SlopworkInbound slopworkInbound, Model model) {
		   
		   // 入库申请
		   SlopworkInbound resultRequest = slopworkInboundService.getClothesRequest(slopworkInbound);
           List<SlopworkInbound> resultRequestList = new ArrayList<SlopworkInbound>();
		    if (resultRequest.getInventoryCategory().equals(ResourcesUtil.getConfig ("clothes_in_category_Samply"))){
                resultRequestList =slopworkInboundService.findShowSamplyRequestDetailList(slopworkInbound);
            }else{
                // 查找入库申请明细
               resultRequestList = slopworkInboundService.findShowRequestDetailList(slopworkInbound);
            }

		   resultRequest.setRequestDetailsList(resultRequestList);
	   	
	       // 将查询结果赋值mvc model层返回jsp页面
	       model.addAttribute("slopworkInbound", resultRequest);
	       
	       // 返回页面地址
	       return "modules/material/slopworkInboundShow";
	   }
	   
		/**
	    * 进入查看页面
	    * @param slopworkInbound
	    * @param model
	    * @return
	    */
	   @RequiresPermissions("slopwork:inbound:view")
	   @RequestMapping(value = "inventoryClothesDetail")
	   public String inventoryClothesDetail(SlopworkInbound slopworkInbound, Model model) {
		   
	   	   // 查找入库申请明细
		   List<SlopworkInbound> resultRequestList = slopworkInboundService.findClothesInventoryDetails(slopworkInbound);
		   slopworkInbound.setClothsInventoryDetailsList(resultRequestList);
	   	
	       // 将查询结果赋值mvc model层返回jsp页面
	       model.addAttribute("slopworkInbound", slopworkInbound);
	       
	       // 返回页面地址
	       return "modules/material/slopworkInboundDetails";
	   }
	   
		/**
		 * 创建物料入库信息窗口
		 * 
		 * @return
		 */
		@RequiresPermissions("slopwork:inbound:view")
		@RequestMapping(value = {"detailCreate"})
		public String detailCreate(SlopworkInbound slopworkInbound,Model model){
			
			// 仓库list
			slopworkInbound.setWarehouseList(slopworkInboundService.findWarehouseList());
			
			model.addAttribute("slopworkInbound",slopworkInbound);
			return "modules/material/slopworkInboundDetailsCreate";
		}	
		
		/**
		 * 创建物料入库信息窗口
		 * 
		 * @return
		 */
		@RequiresPermissions("slopwork:inbound:view")
		@RequestMapping(value = {"findRack"})
		public String findRack(SlopworkInbound slopworkInbound,Model model){
			
			// 货架list
			slopworkInbound.setRackList(slopworkInboundService.findRackList(slopworkInbound));
			
			model.addAttribute("slopworkInbound",slopworkInbound);
			return "modules/material/slopworkInboundDetailsCreate";
		}
		
		/**
		 * 保存信息
		 * @param slopworkInbound
		 * @param response
		 * @return
		 * @throws ResourceException 
		 */
		@RequiresPermissions("slopwork:inbound:edit")
		@RequestMapping(value = "save")
		@ResponseBody
		public String save(SlopworkInbound slopworkInbound, HttpServletResponse response ) throws ResourceException {
						
			// 保存基准数据及子项数据
			slopworkInboundService.saveSlopworkInbound(slopworkInbound);
				
			// 保存成功返回提示信息
		    return renderString(response,AjaxDone.ok(SystemMessageUtils.getMessageName( "slopworkInbound_create_save_success")));
		}
}
